// AppRouter.tsx
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom';
import Login from '../pages/Login/Login';
import AdminLayout from '../pages/Admin/AdminLayout';
import Dashboard from '../pages/Admin/Dashboard';
import TeacherManagement from '../pages/Admin/TeacherManagement';
import StudentManagement from '../pages/Admin/StudentManagement';
import SystemSettings from '../pages/Admin/SystemSettings';
import ProtectedRoute from '../components/ProtectedRoute';
import PermissionProtectedRoute from '../components/PermissionProtectedRoute';
import UserCenter from '../pages/Admin/UserCenter';
import GradeManagement from '../pages/Admin/GradeManagement';
import AdminHome from '../pages/Admin/Home';
import VideoConference from '../pages/Admin/VideoConference';
import VideoCapture from '../video/VideoCapture';
import VideoConRoom from '../pages/Admin/VideoConRoom';
import PermissionManagement from '../pages/Admin/PermissionManagement';
// import removed: Home page was reverted

// 导入学生管理的二级页面组件
import MarketingManagement from '../pages/Admin/students/MarketingManagement';
import ClassManagement from '../pages/Admin/students/ClassManagement';
import CollegeManagement from '../pages/Admin/students/CollegeManagement';
import DormitoryManagement from '../pages/Admin/students/DormitoryManagement';
import KaoQingManagement from '../pages/Admin/students/KaoQingManagement';
import ChatPage from '../pages/Chat/ChatPage';
import AiPage from '../pages/Ai/AiPage';

const AppRouter = () => {
  return (
    <BrowserRouter>
      <Routes>
        {/* 登录页面 */}
        <Route path="/login" element={<Login />} />

        {/* 后台管理路由 - 受保护 */}
        <Route
          path="/admin"
          element={
            <ProtectedRoute>
              <AdminLayout />
            </ProtectedRoute>
          }
        >
          {/* 默认路由指向首页 */}
          <Route index element={<Navigate to="/admin/home" replace />} />
          <Route path="home" element={<AdminHome />} />
          <Route path="dashboard" element={<Dashboard />} />

          {/* 学生管理嵌套路由 - 基于权限 */}
          <Route path="students">
            {/* 学生管理主页 - 需要student:read权限 */}
            <Route index element={<PermissionProtectedRoute requiredPermission="student:read"><StudentManagement /></PermissionProtectedRoute>} />
            {/* 学生管理的各个子页面 - 基于权限 */}
            <Route path="marketing" element={<PermissionProtectedRoute requiredPermission="class:manage"><MarketingManagement /></PermissionProtectedRoute>} />
            <Route path="classes" element={<PermissionProtectedRoute requiredPermission="class:manage"><ClassManagement /></PermissionProtectedRoute>} />
            <Route path="colleges" element={<PermissionProtectedRoute requiredPermission="class:manage"><CollegeManagement /></PermissionProtectedRoute>} />
            <Route path="dormitories" element={<PermissionProtectedRoute requiredPermission="class:manage"><DormitoryManagement /></PermissionProtectedRoute>} />
            <Route path="kaoqing" element={<PermissionProtectedRoute requiredPermission="attendance:read"><KaoQingManagement /></PermissionProtectedRoute>} />
          </Route>

          <Route path="teachers" element={<PermissionProtectedRoute requiredPermission="teacher:read"><TeacherManagement /></PermissionProtectedRoute>} />
          <Route path="grades" element={<PermissionProtectedRoute requiredPermission="grade:read"><GradeManagement /></PermissionProtectedRoute>} />
          <Route path="permissions" element={<PermissionProtectedRoute requiredPermission="role:manage"><PermissionManagement /></PermissionProtectedRoute>} />
          <Route path="video-conference" element={<PermissionProtectedRoute requiredPermission="video:use"><VideoConference /></PermissionProtectedRoute>} />
          <Route path="video-capture" element={<VideoCapture />} />
          <Route path="room/:roomId" element={<VideoConRoom />} />
          <Route path="profile" element={<UserCenter />} />
          <Route path="settings" element={<PermissionProtectedRoute requiredPermission="system:config"><SystemSettings /></PermissionProtectedRoute>} />
        </Route>

        {/* AI聊天页面 */}
        <Route path="/chat" element={<ChatPage />} />
        <Route path="/ai" element={<AiPage />} />

        {/* 默认路由重定向到登录页 */}
        <Route path="/" element={<Navigate to="/login" replace />} />

        {/* 404 路由重定向到登录页 */}
        <Route path="*" element={<Navigate to="/login" replace />} />
      </Routes>
    </BrowserRouter>
  );
};

export default AppRouter;